package top.shiyiri.java;

import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;

/**
 * @author Aunean
 * @date 2022/1/16 14:32
 */
public class Java11Test {

    //Java11新特性：String 中新增的方法
    @Test
    public void test1() {
        //isBlank()：该字符串是否为空白
        System.out.println("\t".isBlank());//true
        System.out.println("    ".isBlank());//true

        //strip()：去除首尾空格
        System.out.println("-----" + "\tabd\n\t".strip() + "---");

        //stripTrailing()：去除尾部的空格
        System.out.println("-----" + "\tabd\n\t".stripTrailing() + "---");

        //stripLeading()：去除首部的空格
        System.out.println("-----" + "\tabd\n\t".stripLeading() + "---");

        //repeat(int count)：重复 count 次
        String str = "abc";
        String repeat = str.repeat(5);
        System.out.println(repeat);//abcabcabcabcabc

        //lines().count()：行数统计
        String str2 = "ab\ncde\tfg";
        System.out.println(str2.lines().count());
    }

    //Java11新特性：Optional的新增方法
    @Test
    public void test2() {

        Optional<Object> op = Optional.empty();
        System.out.println(op.isPresent());//false 判断内部的value是否存在
        System.out.println(op.isEmpty());//true 判断内部的value是否为空

        op = Optional.of("abc");
        //orElseThrow()：value非空，返回value；否则抛异常：NoSuchElementException
        var o = op.orElseThrow();
        System.out.println(o);//abc

        //or：value 非空，返回对应的Optional；value为空，返回形参封装的Optional
        Optional<String> op1 = Optional.of("hello");
        Optional<Object> op2 = op.or(() -> op1);
        System.out.println(op2);//Optional[abc]
    }

    //Java11新特性三：局部变量类型推断的升级
    @Test
    public void test3() {
        //错误的形式：必须要有类型，可以加上var
        //Consumer<String> con1 = (@Deprecated t) -> System.out.println(t.toUpperCase());

        //正确的形式：使用 var 的好处是在使用 Lambda 表达式时给参数加上注解
        Consumer<String> con1 = (@Deprecated var t) -> System.out.println(t.toUpperCase());
    }

    //Java11新特性：HttpClient替换原有的 HttpURLConnection
    @Test
    public void test4() {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(URI.create("http://127.0.0.1:8080/test/")).build();
        HttpResponse.BodyHandler<String> responseBodyHandler = HttpResponse.BodyHandlers.ofString();
        HttpResponse<String> response = null;
        try {
            response = client.send(request, responseBodyHandler);
            String body = response.body();
            System.out.println(body);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test5() {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request =
                HttpRequest.newBuilder(URI.create("http://127.0.0.1:8080/test/")).build();
        HttpResponse.BodyHandler<String> responseBodyHandler = HttpResponse.BodyHandlers.ofString();
        CompletableFuture<HttpResponse<String>> sendAsync =
                client.sendAsync(request, responseBodyHandler);
        sendAsync.thenApply(t -> t.body()).thenAccept(System.out::println);
        HttpResponse<String> response = null;
        try {
            response = sendAsync.get();
            String body = response.body();
            System.out.println(body);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}
